package cn.zzq.aix.nativeinterface.javareflector.parser;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Zhiqiang</br>
 *         用于解析形如a<b,c,d,...>此类字符串，其中getKey方法返回a，本列表中按顺序存放b,c,d,...
 */
public class PatternTextParser extends ArrayList<String> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String key = "";

	public PatternTextParser(String patternText) {
		patternText = patternText.trim();// 清空首尾的空格
		// 正则匹配<>中的内容
		Pattern pattern1 = Pattern.compile("<([^>]+?)>");
		Matcher matcher = pattern1.matcher(patternText);
		// 开始解析
		if (matcher.find()) {
			this.key = patternText.replace(matcher.group(0), "");
			for (String element : matcher.group(1).split(",")) {
				this.add(element.trim());
			}
		}
	}

	public String getKey() {
		return key;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(this.key + '<');
		for (String element : this) {
			sb.append(element + ',');
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append('>');
		return sb.toString();
	}
}
